package com.ludaxu.zhbj;

import java.util.ArrayList;

import com.ludaxu.zhbj.utils.DensityUtils;
import com.ludaxu.zhbj.utils.PrefUtils;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class GuideActivity extends Activity {
	private static final int[] mImages = new int[] { R.drawable.guide_1,
			R.drawable.guide_2, R.drawable.guide_3 };
	private ViewPager vpGuide;
	private ArrayList<ImageView> mImageViewsList;
	private LinearLayout llPointGroup;// 引导圆点的父控件
	private int mPointWidth;// 圆点之间的距离
	private View ViewRedPoint;// 小红点
	private Button btnStart;// 开始体验button

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// 让该Activity没有标题
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);
		llPointGroup = (LinearLayout) findViewById(R.id.ll_point_group);
		vpGuide = (ViewPager) findViewById(R.id.vp_guide);
		ViewRedPoint = findViewById(R.id.view_red_point);
		btnStart = (Button) findViewById(R.id.btn_start);
		btnStart.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 更新sp,表示已经展示过了新手引导页
				PrefUtils.setBoolean(GuideActivity.this,
						"is_user_guide_showed", true);
				// 跳转到主页面,并且关闭当前页面
				// 跳转到主页面
				startActivity(new Intent(GuideActivity.this, MainActivity.class));
				finish();

			}
		});
		initViews();
		vpGuide.setAdapter(new GuideAdapter());
		//
		vpGuide.setOnPageChangeListener(new GuidePageListener());

	}

	/**
	 * 初始化界面,是adapter的数据
	 */
	private void initViews() {
		mImageViewsList = new ArrayList<ImageView>();

		// 初始化引导页的页面
		for (int i = 0; i < mImages.length; i++) {
			ImageView image = new ImageView(this);
			// 设置背景图片
			image.setBackgroundResource(mImages[i]);
			mImageViewsList.add(image);
		}

		// 初始化引导页的小圆点,有多少张图片就添加多少个灰色圆点
		for (int i = 0; i < mImages.length; i++) {
			View point = new View(this);
			// 为点设置参数,点的父控件是什么,我们就设置什么的LayoutParams
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					DensityUtils.dp2px(this, 10), DensityUtils.dp2px(this, 10));
			if (i > 0) {
				params.leftMargin = DensityUtils.dp2px(this, 10);// 从第二个圆点开始间隔左边10dp
			}
			point.setLayoutParams(params);
			// 设置背景图片
			point.setBackgroundResource(R.drawable.shape_point_gray);

			llPointGroup.addView(point);
		}

		// 获取视图树,对layout结束事件
		llPointGroup.getViewTreeObserver().addOnGlobalLayoutListener(
				new OnGlobalLayoutListener() {

					// 当layout执行结束之后会回调此方法
					@Override
					public void onGlobalLayout() {
						System.out.println("layout 结束");
						// 为了防止多次调用,取消监听
						llPointGroup.getViewTreeObserver()
								.removeGlobalOnLayoutListener(this);
						mPointWidth = llPointGroup.getChildAt(1).getLeft()
								- llPointGroup.getChildAt(0).getLeft();
						System.out.println("圆点的距离:" + mPointWidth);
					}
				});

	}

	/**
	 * ViewPager的适配器
	 * 
	 * @author Administrator
	 * 
	 */
	class GuideAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mImageViewsList.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}

		// 初始化
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(mImageViewsList.get(position));
			return mImageViewsList.get(position);
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

	}

	/**
	 * viewpager的滑动监听
	 * 
	 * @author Administrator
	 * 
	 */
	class GuidePageListener implements OnPageChangeListener {

		// 滑动的事件
		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
			// System.out.println("当前位置:" + position + ";百分比:" + positionOffset
			// + ";移动的距离:" + positionOffsetPixels);
			int len = (int) (mPointWidth * positionOffset) + position
					* mPointWidth;
			// 得到当前小红点的布局参数
			RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) ViewRedPoint
					.getLayoutParams();
			params.leftMargin = len;// 设置左边距
			// 重新给小红点设置布局参数
			ViewRedPoint.setLayoutParams(params);

		}

		// 某个页面被选中
		@Override
		public void onPageSelected(int position) {
			if (position == mImages.length - 1) {// 最后一个页面,开始体验按钮显示
				btnStart.setVisibility(View.VISIBLE);
			} else {
				btnStart.setVisibility(View.INVISIBLE);
			}
		}

		// 滑动状态改变
		@Override
		public void onPageScrollStateChanged(int state) {
			// TODO Auto-generated method stub

		}

	}

}
